package com.wufang.lease.web.app.service.impl;

import com.wufang.lease.model.entity.*;
import com.wufang.lease.model.enums.ItemType;
import com.wufang.lease.web.app.mapper.GraphInfoMapper;
import com.wufang.lease.web.app.mapper.LeaseAgreementMapper;
import com.wufang.lease.web.app.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wufang.lease.web.app.vo.agreement.AgreementDetailVo;
import com.wufang.lease.web.app.vo.agreement.AgreementItemVo;
import com.wufang.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    public List<AgreementItemVo> listItemByPhone(String phone) {
        return leaseAgreementMapper.listItemByPhone(phone);
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }

        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());

        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());

        List<GraphVo> apartmentGraphList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        List<GraphVo> roomGraphList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());

        PaymentType paymentTypeInfo = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());

        LeaseTerm leaseTermInfo = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();

        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);

        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphList);
        agreementDetailVo.setRoomGraphVoList(roomGraphList);
        agreementDetailVo.setPaymentTypeName(paymentTypeInfo.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTermInfo.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTermInfo.getUnit());

        return agreementDetailVo;
    }
}




